﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace SendSMSApiSample
{
    class SMS
    {
        public static void SendSMS(string sms, string sendTo, string key, string iv,string certificateId)
        {
            //Make HttpRequest
            string server = "http://sms.s4.itlinkonline.com/sendSMS.aspx";
            string Keystring = key;
            string IVstring = iv;
            string msg = sms;
            string certificate_id = certificateId;


            msg = Encryptmystring(msg + "&send_to=" + sendTo    , Keystring, IVstring);
            
            string URL = server + "?r=" + msg + "&Id=" + certificate_id;
            MakeHTTPRequest(URL);
            //HttpRequest Completed
        }

        private static void MakeHTTPRequest(string URL)
        {
            try
            {
                //HttpRequest
                WebRequest request = WebRequest.Create(URL);
                request.Credentials = CredentialCache.DefaultCredentials;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream datastream = response.GetResponseStream();
                StreamReader reader = new StreamReader(datastream);
                string responsefrmserver = reader.ReadToEnd();
                //MessageBox.Show("Request Completed, status" + response.StatusDescription);
                reader.Close();
                datastream.Close();
                response.Close();
            }
            catch { }
        }
        private static string Encryptmystring(string Message, string Keystring, string IVstring)
        {
            string encrypted = "";
            byte[] MSG = ASCIIEncoding.UTF8.GetBytes(Message);
            byte[] Key = ASCIIEncoding.UTF8.GetBytes(Keystring);
            byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVstring);
            RijndaelManaged rj = new RijndaelManaged();
            //rj.Padding = PaddingMode.Zeros;
            rj.Mode = CipherMode.CBC;
            rj.KeySize = 256;
            rj.BlockSize = 256;


            try
            {
                ICryptoTransform encryptor = rj.CreateEncryptor(Key, IV);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
                cs.Write(MSG, 0, MSG.Length);
                cs.FlushFinalBlock();
                byte[] encoded = ms.ToArray();
                ms.Close();
                cs.Close();
                encrypted = Convert.ToBase64String(encoded);
            }
            catch (CryptographicException e) { Console.WriteLine("Cryptographic Error:" + e.Message); }
            catch (UnauthorizedAccessException e) { Console.WriteLine("File Error:" + e.Message); }
            catch (Exception e) { Console.WriteLine("Unknown Error:" + e.Message); }
            finally { rj.Clear(); }
            return encrypted;
        }

    }
}
